//
//  NSString+FaceReplacing.m
//  yunqiphone
//
//  Created by Irewest on 14-7-9.
//  Copyright (c) 2014年 ci123.com. All rights reserved.
//

#import "NSString+FaceReplacing.h"

@implementation NSString (FaceReplacing)

- (NSString *)replaceFaceWithDictionary:(NSDictionary *)dic
{
    NSString *retString = [NSString stringWithFormat:@"%@", self];
    
    //若没有 [ 符号，则直接返回
    if ([retString rangeOfString:@"["].location == NSNotFound) {
        return retString;
    }
    
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\w+\\]"
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSTextCheckingResult *result = [regex firstMatchInString:retString
                                                     options:0
                                                       range:NSMakeRange(0, [retString length])];
    NSInteger searchedLength;
    while (result) {
        NSString *str = [retString substringWithRange:result.range];
        NSString *face = [dic valueForKey:str];
        if (face.length > 0) {
            //NSString *faceCode = [NSString stringWithFormat:@"%C", (unichar)strtoul([face UTF8String],0,16)];
            NSString *faceCode = [face getFaceCharWithUTF16];
            retString = [retString stringByReplacingCharactersInRange:result.range withString:faceCode];
            searchedLength = result.range.location + faceCode.length;
        }else {
            searchedLength = result.range.location + result.range.length;
        }
        result = [regex firstMatchInString:retString
                                   options:0
                                     range:NSMakeRange(searchedLength, [retString length] - searchedLength)];
    }

    return retString;
}

- (NSString *)getFaceCharWithUTF16
{
    NSString *code = [NSString stringWithFormat:@"%@", self];
    NSString *result;
    if (code.length <= 0) {
        code = @"D83DDE03";
    }
    
    if (self.length == 8) {
        NSString *str1 = [code substringToIndex:4];
        NSString *str2 = [code substringFromIndex:4];
        NSInteger code1 = strtoul([str1 UTF8String],0,16);
        NSInteger code2 = strtoul([str2 UTF8String],0,16);
        result = [NSString stringWithFormat:@"%C%C", (unichar)code1, (unichar)code2];
    }else {
        NSInteger code1 = strtoul([code UTF8String],0,16);
        result = [NSString stringWithFormat:@"%C", (unichar)code1];
    }
    
    return result;
}

- (NSString *)deleteBackWard{
    NSString *text = [NSString stringWithFormat:@"%@", self];
    NSString *result = @"";
    NSString *tmpStr = @"";
    
    if(text.length==0){
        return result;
    }
    
    NSInteger length = text.length;
    
    for (long i=length-1; i>=0; i--) {
        tmpStr=[text substringFromIndex:i];
        
        if([faceKeys containsObject:tmpStr]){
            result=[text substringToIndex:i];
            return result;
        }
    }
    
    result=[text substringToIndex:length-1];
    return result;
}

@end
